﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Windows
{
    /// <summary>
    /// 向量辅助类
    /// </summary>
    public static class VectorHelper
    {
        /// <summary>
        /// 旋转向量指定的弧度得到新的向量
        /// </summary>
        /// <param name="src"></param>
        /// <param name="radian"></param>
        /// <returns></returns>
        public static Vector Rotate(this Vector src, double radian)
        {
            /*
 x1 = x0 * cos（-B） - y0 * sin（-B） =>  x1 = x0 * cosB + y0 * sinB
 y1 = x0 * sin（-B） + y0 * cos（-B）=>  y1 = -x0 * sinB + y0 * cosB
             */

            var x1 = src.X * Math.Cos(radian) + src.Y * Math.Sin(radian);
            var y1 = -src.X * Math.Sin(radian) + src.Y * Math.Cos(radian);
            return new Vector(x1, y1);
        }


        /// <summary>
        /// 获取v2在v1上的投影向量
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public static Vector Projection(this Vector v1, Vector v2)
        {
            var v1l = v1.Length;
            var l = Vector.Multiply(v1, v2) / v1l;
            v1 = AddLength(v1, l - v1l);
            return v1;
        } /// <summary>
          /// 增加向量的长度
          /// </summary>
        public static Vector AddLength(this Vector src, double length_incr)
        {
            var l = src.Length;
            if (l == 0)
            {
                throw new Exception("无方向的向量不能更改其长度。");
            }
            var nl = l + length_incr;
            var b = nl / l;
            double _x = b * src.X;
            double _y = b * src.Y;
            return new Vector(_x, _y);
        }
    }
}
